// Data.h

#ifndef DATA_H
#define DATA_H
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

template<class T> class Data
{
public:
        Data(string path){
                _data.open(path.c_str());//mettre un test pour l'ouverture 
                if(!_data.is_open())
                {
                        cout <<"Cannot open file";
                        //      system("pause");
                }       
                _EOF=false;
                _BOF=true;
                _nbchar=0;
        }
        Data(const Data&){}
        ~Data(void){
                _data.close();
                //cout << "test";
        } 
        void Close(void){
                _data.close();
        }
        Data& operator=(const Data&){}
string  Get_char(){
        char current_char='\0';//onfixe le current char egale null
        streambuf *pbuf;// on initialise pointeur sur streambuf
        pbuf=_data.rdbuf();// on aloue le pointeur sur les donnés (voir constructeur)
        _nbchar++;
        if(pbuf->sgetc()!=EOF)//test la fin du fichier
        {
                _data >> current_char;
        }else{
                _EOF=true;
        }
        if(_nbchar>1) _BOF=false;
        stringstream ss;
        ss << current_char;
        string s;
         ss >> s;
        return s;
}
string Get_line(void)
{
        string s=" ";
        if(getline(_data,s))//test la fin du fichier
        {
                //cout << "->" << s << "<-";
        }else{
                _EOF=true;
        }
        return s;
}
void  Get_dim(int *dim)
{
	int i=0;
	int n=0;
	int lines=0;
	int spaces=0;
	while(!(_EOF))
	{
		string S=Get_line(); 	
		if(i==0)
		{
			spaces=count(S.begin(),S.end(),' '); 			
			i++;
		}
		lines++;
	}
	dim[0]=spaces+1;
	dim[1]=lines-1;
}

void Get_table(int *dim)
{
	double *table=new double[dim[0]*dim[1]];
	_data.clear();
	_EOF=false;
	unsigned int pos;
	unsigned int ppos=0;
	_data.seekg(0,ios::beg);
	int k=0;
	std::istringstream stm;
	while(!(_EOF))
	{
		string S=Get_line(); 
		int n=S.length();
		pos=S.find(' ');
		cout << pos;
		while(pos<n)	
		{
			stm.str(S.substr(ppos,pos-ppos));
			stm >> table[k];
			ppos=pos;
			pos=S.find(' ',pos);
			//cout << table[k] << " ";
			k++;
		}	
		//cout << "\n";

	}
	delete[] table;
}

bool Test_EOF(){
                return _EOF;
}
bool Test_BOF(){

        return _BOF;
}

void Write(string c){
        _data << c;
                //cout << c << "\n";
}
void WriteNumber(int i){
        _data << i;
}

private:
        T _data; //file type
        bool _EOF;
        bool _BOF;
        int _nbchar;
}; 
#endif // 

